package org.jeecg.modules.yc.web.controller;

import java.util.Arrays;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.PermissionData;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;


import org.jeecg.modules.yc.web.entity.DeviceReport;
import org.jeecg.modules.yc.web.entity.HourTimeLine;
import org.jeecg.modules.yc.web.entity.vo.HourTimeExportXls;
import org.jeecg.modules.yc.web.service.IDeviceReportService;

import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.apache.shiro.authz.annotation.RequiresPermissions;

 /**
 * @Description: 上报数据
 * @Author: jeecg-boot
 * @Date:   2023-12-26
 * @Version: V1.0
 */
@Api(tags="上报数据")
@RestController
@RequestMapping("/yc/web/deviceReport")
@Slf4j
public class DeviceReportController extends JeecgController<DeviceReport, IDeviceReportService> {
	@Autowired
	private IDeviceReportService deviceReportService;

	/**
	 * 分页列表查询
	 *
	 * @param deviceReport
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "上报数据-分页列表查询")
	@ApiOperation(value="上报数据-分页列表查询", notes="上报数据-分页列表查询")
	@GetMapping(value = "/list")
	@PermissionData(pageComponent="yc/deviceReport/DeviceReportList")
	public Result<IPage<DeviceReport>> queryPageList(DeviceReport deviceReport,
													 @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
													 @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
													 HttpServletRequest req) {
		QueryWrapper<DeviceReport> queryWrapper = QueryGenerator.initQueryWrapper(deviceReport, req.getParameterMap());
		Page<DeviceReport> page = new Page<DeviceReport>(pageNo, pageSize);
		IPage<DeviceReport> pageList = deviceReportService.page(page, queryWrapper);
		return Result.OK(pageList);
	}

	/**
	 *   添加
	 *
	 * @param deviceReport
	 * @return
	 */
	@AutoLog(value = "上报数据-添加")
	@ApiOperation(value="上报数据-添加", notes="上报数据-添加")
	@RequiresPermissions("yc/web:device_report:add")
	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody DeviceReport deviceReport) {
		deviceReportService.save(deviceReport);
		return Result.OK("添加成功！");
	}

	/**
	 *  编辑
	 *
	 * @param deviceReport
	 * @return
	 */
	@AutoLog(value = "上报数据-编辑")
	@ApiOperation(value="上报数据-编辑", notes="上报数据-编辑")
	@RequiresPermissions("yc/web:device_report:edit")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody DeviceReport deviceReport) {
		deviceReportService.updateById(deviceReport);
		return Result.OK("编辑成功!");
	}

	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "上报数据-通过id删除")
	@ApiOperation(value="上报数据-通过id删除", notes="上报数据-通过id删除")
	@RequiresPermissions("yc/web:device_report:delete")
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		deviceReportService.removeById(id);
		return Result.OK("删除成功!");
	}

	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "上报数据-批量删除")
	@ApiOperation(value="上报数据-批量删除", notes="上报数据-批量删除")
	@RequiresPermissions("yc/web:device_report:deleteBatch")
	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.deviceReportService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "上报数据-通过id查询")
	@ApiOperation(value="上报数据-通过id查询", notes="上报数据-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<DeviceReport> queryById(@RequestParam(name="id",required=true) String id) {
		DeviceReport deviceReport = deviceReportService.getById(id);
		if(deviceReport==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(deviceReport);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param deviceReport
    */
    @RequiresPermissions("yc/web:device_report:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, DeviceReport deviceReport) {
        return super.exportXls(request, deviceReport, DeviceReport.class, "上报数据");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequiresPermissions("yc/web:device_report:importExcel")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, DeviceReport.class);
    }

	 @ApiOperation(value="上报数据-分页列表查询", notes="上报数据-分页列表查询")
	 @GetMapping(value = "/deviceHourTimeLineData")
	 @PermissionData(pageComponent="yc/deviceReport/deviceHourTimeLineData")
	 public Result<HourTimeLine> deviceHourTimeLineData(DeviceReport deviceReport,
														HttpServletRequest req) {
		 QueryWrapper<DeviceReport> queryWrapper = QueryGenerator.initQueryWrapper(deviceReport, req.getParameterMap());
		 HourTimeLine hourTimeLine = deviceReportService.deviceHourTimeLineData(queryWrapper);
		 return Result.OK(hourTimeLine);
	 }


	 /**
	  * 通过excel导入数据
	  * @return
	  */
	 @RequestMapping(value = "/HourTimeExportXls")
	 public ModelAndView HourTimeExportXls(DeviceReport deviceReport,
										HttpServletRequest req) {
		 QueryWrapper<DeviceReport> queryWrapper = QueryGenerator.initQueryWrapper(deviceReport, req.getParameterMap());
		 List<HourTimeExportXls> hourTimeExportXls = deviceReportService.HourTimeExportXls(queryWrapper);
		 return super.dataExportXls(hourTimeExportXls,HourTimeExportXls.class,"分时上报数据");
	 }
}
